
package com.platform.modules.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.utils.Constant;
import com.platform.common.utils.Query;
import com.platform.modules.mall.dao.MallUserIntegralLogDao;
import com.platform.modules.mall.entity.MallUserEntity;
import com.platform.modules.mall.entity.MallUserIntegralLogEntity;
import com.platform.modules.mall.service.MallUserIntegralLogService;
import com.platform.modules.mall.service.MallUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * USER_INTEGRAL_LOG
 * 用户积分记录表Service实现类
 *
 * @author
 * @date 2021-12-16 11:45:21
 */
@Service("mallUserIntegralLogService")
public class MallUserIntegralLogServiceImpl extends ServiceImpl<MallUserIntegralLogDao, MallUserIntegralLogEntity> implements MallUserIntegralLogService {

    @Resource
    MallUserService userService;

    @Override
    public List<MallUserIntegralLogEntity> queryAll(Map<String, Object> params) {
        return baseMapper.queryAll(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.ADD_TIME");
        params.put("asc", false);
        Page<MallUserIntegralLogEntity> page = new Query<MallUserIntegralLogEntity>(params).getPage();
        return page.setRecords(baseMapper.selectMallUserIntegralLogPage(page, params));
    }

    @Override
    public boolean integralChange(MallUserIntegralLogEntity entity) {
        if(entity.getAddTime() == null){
            entity.setAddTime(new Date());
        }
        if(entity.getOldIntegral() == null){
            entity.setOldIntegral(userService.lambdaQuery()
                    .select(MallUserEntity::getId,MallUserEntity::getSignAllIntegral)
                    .eq(MallUserEntity::getId,entity.getUserId())
                    .one().getSignAllIntegral());
        }
        return this.save(entity);
    }

    @Override
    public boolean add(String userId, BigDecimal integral, BigDecimal oldIntegral, Constant.IntegralType type, String details) {
        MallUserIntegralLogEntity mallUserIntegralLogEntity = new MallUserIntegralLogEntity();
        mallUserIntegralLogEntity.setUserId(userId);
        mallUserIntegralLogEntity.setIntegral(integral);
        mallUserIntegralLogEntity.setOldIntegral(oldIntegral);
        mallUserIntegralLogEntity.setType(type.getValue());
        mallUserIntegralLogEntity.setDetails(details);
        return this.save(mallUserIntegralLogEntity);
    }

    @Override
    public boolean update(MallUserIntegralLogEntity mallUserIntegralLog) {
        return this.updateById(mallUserIntegralLog);
    }

    @Override
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(String[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }
}
